package edu.hawaii.halealohacli.kmj;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.junit.Test;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Test cases for the RankTowers class.
 * Uses http://server.wattdepot.org:8190/wattdepot/ as the server.
 * 
 * @author Micah Takabayashi
 */
public class TestRankTowers {

  private WattDepotClient client;
  
  /**
   * Constructor.
   */
  public TestRankTowers() {
    client = new WattDepotClient("http://server.wattdepot.org:8190/wattdepot/");
  }
  
  /**
   * Tests that sources obtained by class are correct by checking their names.
   * @throws DatatypeConfigurationException 
   */
  @Test
  public void testGetSources() throws DatatypeConfigurationException {
    
    Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.US);
    XMLGregorianCalendar startTime =
        DatatypeFactory.newInstance().newXMLGregorianCalendar(1, 1, 1, 0, 0, 0, 0, 0);
    XMLGregorianCalendar endTime =
        DatatypeFactory.newInstance().newXMLGregorianCalendar(1, 1, 1, 0, 0, 0, 0, 0);
    calendar.add(Calendar.DATE, -1);
    
    
    endTime.setDay(calendar.get(Calendar.DATE));
    endTime.setYear(calendar.get(Calendar.YEAR));
    endTime.setMonth(calendar.get(Calendar.MONTH) + 1);
    
    calendar.add(Calendar.DATE, -1);
    startTime.setDay(calendar.get(Calendar.DATE));
    startTime.setYear(calendar.get(Calendar.YEAR));
    startTime.setMonth(calendar.get(Calendar.MONTH) + 1);
    
    RankTowers rank = new RankTowers(client, startTime, endTime);
    
    Source[] sources = rank.getSources();
    assertEquals("First source (Mokihana) incorrect.", "Mokihana", sources[0].getName());
    assertEquals("Second source (Ilima) incorrect.", "Ilima", sources[1].getName());
    assertEquals("Third source (Lehua) incorrect.", "Lehua", sources[2].getName());
    assertEquals("Fourth source (Lokelani) incorrect.", "Lokelani", sources[3].getName());
  }
  
  /**
   * Tests that class's getEnergy method is returning logical results.
   * Checks that energy usage returned for a 2-day span is greater than
   * usage returned for a 1-day span.
   * @throws DatatypeConfigurationException 
   * @throws MiscClientException 
   * @throws BadXmlException 
   * @throws ResourceNotFoundException 
   * @throws NotAuthorizedException 
   */
  @Test
  public void testGetEnergy() throws DatatypeConfigurationException, NotAuthorizedException,
  ResourceNotFoundException, BadXmlException, MiscClientException {
    Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.US);
    XMLGregorianCalendar startTime =
        DatatypeFactory.newInstance().newXMLGregorianCalendar(1, 1, 1, 0, 0, 0, 0, 0);
    XMLGregorianCalendar endTime =
        DatatypeFactory.newInstance().newXMLGregorianCalendar(1, 1, 1, 0, 0, 0, 0, 0);
    
    
    endTime.setDay(calendar.get(Calendar.DATE));
    endTime.setYear(calendar.get(Calendar.YEAR));
    endTime.setMonth(calendar.get(Calendar.MONTH) + 1);
    
    calendar.add(Calendar.DATE, -1);
    startTime.setDay(calendar.get(Calendar.DATE));
    startTime.setYear(calendar.get(Calendar.YEAR));
    startTime.setMonth(calendar.get(Calendar.MONTH) + 1);
    
    RankTowers rank = new RankTowers(client, startTime, endTime);
    
    Source[] sources = rank.getSources();
    
    int oneDay = rank.getEnergy(sources[0], startTime, endTime);
    
    calendar.add(Calendar.DATE, -1);
    startTime.setDay(calendar.get(Calendar.DATE));
    startTime.setYear(calendar.get(Calendar.YEAR));
    startTime.setMonth(calendar.get(Calendar.MONTH) + 1);
    
    int twoDays = rank.getEnergy(sources[0], startTime, endTime);
    
    assertTrue("Return values failed sanity check.", twoDays >= oneDay);
  }
}




